from aiohttp import web
import aiohttp_jinja2
import jinja2
from pathlib import Path
import views

here = Path(__file__).resolve().parent

app = web.Application()
routers = web.RouteTableDef()


def setup_app():
    aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(str(here)+"/pages"))

@routers.get("/")
@aiohttp_jinja2.template("index.html")
async def index(request):
    return {}

@routers.get("/json")
async def json_response(request):
    return web.json_response({"name": ["huzheng", "huang", "tian"]})


def gen_app():
    setup_app()
    app.add_routes(routers)
    app.router.add_static("/i/", str(here) + "/imgs/")
    app.router.add_static("/s/", str(here) + "/statics/")
    for view in views.__all__:
        app.add_subapp("/"+view, getattr(views, view).app)
    return app


if __name__ == "__main__":
    app = gen_app()
    web.run_app(app, port=12345)